package com.example.l2p_feedback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;

public class View01_OverallFeedback extends Activity {
	ExpandableListAdapter listAdapter;
	ExpandableListView expListView;
	List<String> listDataHeader;
	HashMap<String, List<String>> listDataChild;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view01_listview);

		this.setTitle("Overall Feedback Collection");
		
		// preparing list data
		prepareListData();

		listAdapter = new ExpandableListAdapter(this, listDataHeader,
				listDataChild);

		// setting list adapter
		expListView = (ExpandableListView) findViewById(R.id.view01_lvExp);
		expListView.setAdapter(listAdapter);

		Button button = (Button) findViewById(R.id.view01_btnOverallFeedback);

		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Create the intent to start another activity
				// Intent i = new Intent(View01_OverallFeedback.this,
				// MainActivity.class);
				// startActivity(i);
				Context context = View01_OverallFeedback.this;
				AlertDialog.Builder alert = new AlertDialog.Builder(context);

				alert.setTitle("OverallFeedback on: 'DEMO COURSE'");
				alert.setMessage("Format: rating#content. 0==poor, 1==avrg, 2==good");

				// Set an EditText view to get user input
				final EditText input = new EditText(context);
				alert.setView(input);

				alert.setPositiveButton("Ok",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								String value = input.getText().toString();
								// Toast.makeText(getApplicationContext(),
								// value,
								// Toast.LENGTH_LONG).show();
								// Do something with value!
								if (listDataHeader.size() == 4) {
									List<String> demoFeedback = listDataChild
											.get(listDataHeader.get(3));
									demoFeedback.add(value);
									listDataChild.put(listDataHeader.get(3),
											demoFeedback);
								} else {
									listDataHeader.add("DEMO COURSE");
									List<String> demoFeedback = new ArrayList<String>();
									demoFeedback.add(value);
									listDataChild.put(listDataHeader.get(3),
											demoFeedback);
								}
								expListView.setAdapter(listAdapter);
							}
						});

				alert.show();
				// see
				// http://www.androidsnippets.com/prompt-user-input-with-an-alertdialog

			}
		});
	}

	/*
	 * Preparing the list data
	 */
	private void prepareListData() {
		listDataHeader = new ArrayList<String>();
		listDataChild = new HashMap<String, List<String>>();

		// Adding child data
		listDataHeader.add("eLearning");
		listDataHeader.add("Advanced Learning Technology");
		listDataHeader.add("Web Technology");

		// Adding child data
		List<String> eLearning = new ArrayList<String>();
		eLearning.add("2#It was good course where I learnt a lot.");
		eLearning.add("0#I am not happy with the course.");

		List<String> advLT = new ArrayList<String>();
		advLT.add("2#State of the art course content");
		advLT.add("2#Groupwork was fun.");

		List<String> webTechnology = new ArrayList<String>();
		webTechnology.add("2#Real life usage.");
		webTechnology.add("1#Overal: OK");
		webTechnology.add("0#Lots of theory");
		listDataChild.put(listDataHeader.get(0), eLearning); // Header, Child
																// data
		listDataChild.put(listDataHeader.get(1), advLT);
		listDataChild.put(listDataHeader.get(2), webTechnology);
	}
}